//
//  HDCustomAlertView.m
//  Weshare
//
//  Created by Mike on 16/4/28.
//  Copyright © 2016年 wxwl. All rights reserved.
//

#import "HDCustomAlertView.h"

@interface HDCustomAlertView ()
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *content;
@property(nonatomic,strong)NSAttributedString *attributeContent;
@property(nonatomic,strong)UIView *backgroundView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UITextView *contentView;
@end

@implementation HDCustomAlertView

- (id)initWithTitle:(NSString *)title content:(NSString *)content
{
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineSpacing = 8;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"STHeitiSC-Light" size:16],NSForegroundColorAttributeName:[UIColor blackColor],NSParagraphStyleAttributeName:paragraph};
    NSAttributedString *contentStr = [[NSAttributedString alloc] initWithString:[HDUtil check:content] attributes:attributes];
    self.attributeContent = contentStr;
    return [self initWithTitle:title attributeContent:self.attributeContent];
}

- (id)initWithTitle:(NSString *)title attributeContent:(NSAttributedString *)attributeContent
{
    self = [super initWithFrame:CGRectMake(20, 40, SCREEN_WIDTH-40, SCREEN_HEIGHT-80)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.title = [HDUtil check:title];
        self.attributeContent = attributeContent;
        self.layer.cornerRadius = 5;
        self.clipsToBounds = YES;
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, self.width-60, 40)];
        self.titleLabel.textColor = kDarkGrayColor;
        self.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        self.titleLabel.text = self.title;
        [self addSubview:self.titleLabel];
        
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom, self.width, 0.5)];
        line.image = [UIImage imageNamed:@"dot"];
        [self addSubview:line];
        
        self.contentView = [[UITextView alloc] initWithFrame:CGRectMake(5, line.bottom+10, self.width-10, self.height-40)];
        self.contentView.editable = NO;
        self.contentView.selectable = NO;
        self.contentView.showsVerticalScrollIndicator = NO;
        self.contentView.attributedText = self.attributeContent;
//        self.contentView.textAlignment = NSTextAlignmentCenter;
        CGFloat contentHeight = [HDUtil heightForTextViewSize:CGSizeMake(self.width-10, CGFLOAT_MAX) withAttributeText:self.attributeContent];
        self.contentView.height = contentHeight<self.height-40?contentHeight+5:self.height-40;
        self.height = self.contentView.bottom+65;
        self.top = (SCREEN_HEIGHT-self.height)/2.0;
        [self addSubview:self.contentView];
        
        GXButton *cancelButton = [[GXButton alloc] initWithFrame:CGRectMake((self.width-100)/2, self.contentView.bottom+5, 100, 40)];
        [cancelButton setTitle:@"确认" forState:UIControlStateNormal];
        cancelButton.titleLabel.font = HeitiFont(14);
        cancelButton.layer.cornerRadius = 2.0f;
        cancelButton.backgroundColor = kYellowColor;
        [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelButton];
        
    }
    return self;
}

- (void)show
{
    if (!self.backgroundView) {
        self.backgroundView = [[UIView alloc] initWithFrame:SCREENFRAME];
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAction)];
        tapGesture.delegate = (id)self;
        [self.backgroundView addGestureRecognizer:tapGesture];
    }
    if (!self.backgroundView.superview) {
        UIWindow *window = [UIApplication sharedApplication].windows[0];
        [window addSubview:self.backgroundView];
    }
    if (!self.superview) {
        [self.backgroundView addSubview:self];
    }
    self.top = SCREEN_HEIGHT;
    self.backgroundView.alpha = 0;
    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.backgroundView.alpha = 1;
        self.top = (SCREEN_HEIGHT-self.height)/2.0;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)cancelAction
{
    [UIView animateWithDuration:0.2 animations:^{
        self.top = SCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.backgroundView.alpha = 0;
        } completion:^(BOOL finished) {
            [self.backgroundView removeFromSuperview];
        }];
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.backgroundView];
    return !CGRectContainsPoint(self.frame, touchPoint);
}

@end
